package com.mc.cloud.mall.cartOrder.controller;

import com.github.pagehelper.PageInfo;
import com.mc.cloud.mall.cartOrder.model.vo.CreateOrder;
import com.mc.cloud.mall.cartOrder.model.vo.OrderVO;
import com.mc.cloud.mall.cartOrder.service.OrderService;
import com.mc.cloud.mall.common.common.MallRestResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName OrderController
 * @Author MC
 * @Date 2020/11/9 12:24
 * @Version 1.0
 **/
@RestController
public class OrderController {
    
    @Autowired
    OrderService orderService;
    
    /**
     * 创建订单
     * @param createOrder
     * @return
     */
    @PostMapping("/order/create")
    @ApiOperation("创建订单")
    public MallRestResponse create(@Valid @RequestBody CreateOrder createOrder){
    
        String orderNo = orderService.create(createOrder);
        return MallRestResponse.success(orderNo);
    }
    
    /**
     * 前台订单详情
     * @param orderNo
     * @return
     */
    @GetMapping("/order/detail")
    @ApiOperation("前台订单详情")
    public MallRestResponse detail(@RequestParam String orderNo){
        OrderVO orderVO = orderService.detail(orderNo);
        return MallRestResponse.success(orderVO);
    }
    
    /**
     * 前台订单详情
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/order/list")
    @ApiOperation("前台订单详情")
    public MallRestResponse list(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageInfo pageInfo = orderService.orderListForCustom(pageNum, pageSize);
        return MallRestResponse.success(pageInfo);
    }
    
    /**
     * 前台取消订单
     * @param orderNo
     * @return
     */
    @PostMapping("/order/cancel")
    @ApiOperation("前台取消订单")
    public MallRestResponse cancel(@RequestParam String orderNo){
        orderService.cancel(orderNo);
        return MallRestResponse.success();
    }
    
    /**
     * 生成支付二维码
     * @param orderNo
     * @return
     */
    @GetMapping("/order/qrcode")
    @ApiOperation("生成支付二维码")
    public MallRestResponse qrcode(@RequestParam String orderNo){
        
        String pngAddress = orderService.qrcode(orderNo);
        return MallRestResponse.success(pngAddress);
    }
    
    /**
     * 支付
     * @param orderNo
     * @return
     */
    @PostMapping("/order/pay")
    @ApiOperation("支付")
    public MallRestResponse pay(@RequestParam String orderNo){
        orderService.pay(orderNo);
        return MallRestResponse.success();
    }
}
